const express = require('express')
const path = require('path')
const cookieParser = require('cookie-parser')
const logger = require('morgan')
require('dotenv').config()
const app = express()
const cors = require('cors')
app.use(cors())
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))

// 前台接口文件
const userAuth = require('./middlewares/user-auth')
const indexRouter = require('./routes/index')
const categoriesRouter = require('./routes/categories')
const coursesRouter = require('./routes/courses')
const chaptersRouter = require('./routes/chapters')
const articlesRouter = require('./routes/articles')
const settingsRouter = require('./routes/settings')
const searchRouter = require('./routes/search')
const usersRouter = require('./routes/users')
const authRouter = require('./routes/auth')
const likesRouter = require('./routes/likes')

// 前台接口配置
app.use('/', indexRouter)
app.use('/categories', categoriesRouter)
app.use('/courses', coursesRouter)
app.use('/chapters', chaptersRouter)
app.use('/articles', articlesRouter)
app.use('/settings', settingsRouter)
app.use('/search', searchRouter)
app.use('/users', userAuth, usersRouter)
app.use('/auth', authRouter)
app.use('/likes', userAuth, likesRouter)

// 后台接口文件
const adminAuth = require('./middlewares/admin-auth')
const adminArticlesRouter = require('./routes/admin/articles')
const adminCategoriesRouter = require('./routes/admin/categories')
const adminSettingsRouter = require('./routes/admin/settings')
const adminUsersRouter = require('./routes/admin/users')
const adminCoursesRouter = require('./routes/admin/courses')
const adminChaptersRouter = require('./routes/admin/chapters')
const adminChartsRouter = require('./routes/admin/charts')
const adminAuthRouter = require('./routes/admin/auth')

// 后台接口配置
app.use('/admin/articles', adminAuth, adminArticlesRouter)
app.use('/admin/categories', adminAuth, adminCategoriesRouter)
app.use('/admin/settings', adminAuth, adminSettingsRouter)
app.use('/admin/users', adminAuth, adminUsersRouter)
app.use('/admin/courses', adminAuth, adminCoursesRouter)
app.use('/admin/chapters', adminAuth, adminChaptersRouter)
app.use('/admin/charts', adminAuth, adminChartsRouter)
app.use('/admin/auth', adminAuthRouter)

module.exports = app
